<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>列表渲染</h2>
      <h3>数组的循环</h3>
      <ul>
        <li v-for="(item, index) in list">{{ item.name }} - {{ index }}</li>
        <!-- <li v-for="({name}, index) in list">{{ name }} - {{ index }}</li> -->
      </ul>

      <!-- v-for是可以嵌套的 -->
      <div v-for="item of list">
        <div>{{ item.name }}</div>
        <div v-for="value of item.children">{{ value.name }}</div>
      </div>

      <hr />

      <h3>对象的循环</h3>
      <!-- 对象的循环有三个参数value, key, index -->
      <ul>
        <li v-for="(value, key, index) in myObject">
          {{value}} - {{ key }} - {{ index }}
        </li>
      </ul>

      <hr />

      <h3>范围值</h3>
      <!-- in一个数字5的时候，相当于in [1,2,3,4,5] -->
      <ul>
        <li v-for="item in 5">{{ item }}</li>
      </ul>

      <div v-for="item in 9">
        <span v-for="val in item" style="display: inline-block; width: 120px">
          {{ item }} * {{ val }} = {{ item * val }}
        </span>
      </div>

      <hr />

      <h3>template上使用v-for</h3>

      <ul>
        <template v-for="item in list">
          <li>name: {{ item.name }}</li>
          <li>id: {{ item.id }}</li>
        </template>
      </ul>
    </div>

    <script src="./lib/vue3.js"></script>
    <script>
      const app = Vue.createApp({
        data() {
          return {
            list: [
              {
                id: 1,
                name: "zhangsan",
                children: [
                  {
                    id: 11,
                    name: "zs1",
                  },
                  {
                    id: 12,
                    name: "zs2",
                  },
                ],
              },
              {
                id: 2,
                name: "lisi",
                children: [
                  {
                    id: 21,
                    name: "ls1",
                  },
                  {
                    id: 22,
                    name: "ls2",
                  },
                ],
              },
            ],
            myObject: {
              title: "How to do lists in Vue",
              author: "Jane Doe",
              publishedAt: "2016-04-10",
            },
          };
        },
      }).mount("#app");
    </script>
  </body>
</html>
